/**
 * This class is designed to be used in both website and android.
 * <p/>
 * zhaoxu @ 2012-05-16 11:24
 * <p/>
 */
package com.threeti.mecool.core.domain.model.plan.pojo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonManagedReference;
import org.codehaus.jackson.annotate.JsonTypeInfo;

public class Job {
	private Long id;
	private Integer version;
	private String name;
	private Step firstStep;
	private transient Long firstStepId;
	private List<Step> steps = new ArrayList<Step>(0);

	/**
	 * @return unmodifiableList
	 */
	@JsonManagedReference
	@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS)
	public List<Step> getSteps() {
		return Collections.unmodifiableList(steps);
	}
	
	public Step findStepById(Long stepId) {
		if (stepId == null) {
			return null;
		}
		for (Step step : steps) {
			if (stepId.equals(step.getId())) {
				return step;
			}
		}
		return null;
	}

	public void addStep(Step step) {
		step.setJob(this);
		steps.add(step);
	}
	
	public void insertStep(Step step) {
		step.setJob(this);
		steps.add(0, step);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@JsonIgnore
	public Step getFirstStep() {
		if (firstStep == null && firstStepId != null) {
			firstStep = findStepById(firstStepId);
		}
		return firstStep;
	}

	public void setFirstStep(Step firstStep) {
		this.firstStep = firstStep;
	}

	public Long getFirstStepId() {
		if (firstStepId == null && firstStep != null) {
			firstStepId = firstStep.getId();
		}
		return firstStepId;
	}
	
	public void setFirstStepId(Long val) {
		firstStepId = val;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}
	
}
